import 'package:boss/provider/table_provider.dart';
import 'package:boss/ui/page/tableManagement/page/areaManagement/page/areaDetail/area_detail_page.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';
import 'package:boss/ui/widget/radioList/radio.dart' as radio;
import 'package:hi_common/hi_common.dart';

@RouterConfig(PickerAreaPage.pageName)
class PickerAreaPage extends StatefulWidget {
  static const pageName = '/PickerAreaPage';

  @Autowired()
  final int selectedAreaId;

  const PickerAreaPage({this.selectedAreaId, Key key}) : super(key: key);

  @override
  _PickerAreaPageState createState() => _PickerAreaPageState();
}

class _PickerAreaPageState extends State<PickerAreaPage> {
  int selectedAreaId;

  @override
  void initState() {
    super.initState();
    selectedAreaId = widget.selectedAreaId;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '选择区域',
        actions: [
          LabelButton(
            title: '确定',
            padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
            onTap: () {
              final provider = Provider.of<TableProvider>(context, listen: false);
              final area = provider.storeAreas.firstWhere((element) => element.areaId == selectedAreaId, orElse: () => null);
              Navigator.pop(context, area);
            },
          ),
        ],
      ),
      body: Column(
        children: [
          Expanded(
            child: Consumer<TableProvider>(
              builder: (context, provider, _) {
                return ListView.builder(
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  itemBuilder: (context, index) {
                    final current = provider.storeAreas[index];
                    final isSelected = current.areaId == selectedAreaId;

                    return CustomListTitle(
                      title: current.areaName,
                      trailing: radio.Radio(isSelected: isSelected),
                      onTap: () {
                        setState(() {
                          selectedAreaId = current.areaId;
                        });
                      },
                    );
                  },
                  itemCount: provider.storeAreas.length,
                );
              },
            ),
          ),
          FixedBottomBtn(
            title: '新增区域',
            onTap: () {
              Navigator.pushNamed(context, AreaDetailPage.pageName);
            },
          ),
        ],
      ),
    );
  }
}
